<template>
  <div>
    <el-dialog v-bind="$attrs" v-on="$listeners" @open="onOpen" @close="onClose" title="添加用户" width="40%">
      <el-row :gutter="15">
        <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
          <el-col :span="24">
            <el-form-item label="账号" prop="userName">
              <el-input v-model="formData.userName" placeholder="请输入账号" clearable :style="{width: '100%'}">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="用户名" prop="nickName">
              <el-input v-model="formData.nickName" placeholder="请输入用户名" clearable :style="{width: '100%'}">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="密码" prop="password">
              <el-input v-model="formData.password" placeholder="请输入密码" clearable show-password
                        :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="手机号" prop="phonenumber">
              <el-input v-model="formData.phonenumber" placeholder="请输入手机号" clearable :style="{width: '100%'}">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="性别" prop="sex">
              <el-select v-model="formData.sex" placeholder="请选择性别" clearable :style="{width: '100%'}">
                <el-option v-for="(item, index) in field105Options" :key="index" :label="item.label"
                           :value="item.value" :disabled="item.disabled"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="13">
            <el-form-item label="身份" prop="userType">
              <el-select v-model="formData.userType" placeholder="请选择身份" clearable :style="{width: '100%'}">
                <el-option v-for="(item, index) in field106Options" :key="index" :label="item.label"
                           :value="item.value" :disabled="item.disabled"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="账号状态" prop="status" required>
              <el-switch v-model="formData.status"></el-switch>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {addUser} from "@/api/user";

export default {
  inheritAttrs: false,
  components: {},
  props: [],
  data() {
    return {
      formData: {
        userName: undefined,
        nickName: undefined,
        password: undefined,
        phonenumber: undefined,
        sex: 0,
        userType: 1,
        status: true
      },
      rules: {
        userName: [{
          required: true,
          message: '请输入账号',
          trigger: 'blur'
        }],
        nickName: [{
          required: true,
          message: '请输入用户名',
          trigger: 'blur'
        }],
        password: [{
          required: true,
          message: '请输入密码',
          trigger: 'blur'
        }],
        phonenumber: [{
          required: true,
          message: '请输入手机号',
          trigger: 'blur'
        }],
        sex: [{
          required: true,
          message: '请选择性别',
          trigger: 'change'
        }],
        userType: [{
          required: true,
          message: '请选择身份',
          trigger: 'change'
        }],
      },
      field105Options: [{
        "label": "男",
        "value": 0
      }, {
        "label": "女",
        "value": 1
      }],
      field106Options: [{
        "label": "教师",
        "value": 1
      }, {
        "label": "学生",
        "value": 2
      }],
    }
  },
  methods: {
    onOpen() {},
    onClose() {
      this.$refs['elForm'].resetFields()
    },
    close() {
      this.$emit('update:visible', false)
    },
    handelConfirm() {
      this.$refs['elForm'].validate(valid => {
        if (!valid) return
        this.formData.status=this.formData.status===true?0:1;
        addUser(this.formData).then(res=>{
          if (res.code===200){
            this.$emit("addResult",true)
            this.$message.success("添加成功");
          }
        })
        this.close()
      })
    },
  }
}

</script>
<style>
</style>
